package com.spider.util;

import com.alibaba.fastjson.JSONObject;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Map;


public class HttpTokenUtil {

    public static String postRequest(HttpHeaders httpHeaders,String url, MultiValueMap<String, Object> map) throws Exception {
        SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
        requestFactory.setConnectTimeout(10000);// 设置建立连接超时时间
        requestFactory.setReadTimeout(10000);// 设置等待返回超时时间

        RestTemplate restTemplate = new RestTemplate(requestFactory);
        HttpMethod method = HttpMethod.POST;
        ResponseEntity<String> response = restTemplate.postForEntity(url, map,String.class);
        return response.getBody();
    }

    public static String postRequest(HttpHeaders httpHeaders,String url, JSONObject params) throws Exception {
        SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
        requestFactory.setConnectTimeout(10000);// 设置建立连接超时时间
        requestFactory.setReadTimeout(10000);// 设置等待返回超时时间

        RestTemplate client = new RestTemplate(requestFactory);
        HttpMethod method = HttpMethod.POST;
        //httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        HttpEntity<JSONObject> requestEntity = new HttpEntity<>(params, httpHeaders);
        ResponseEntity<String> response = client.exchange(url, method, requestEntity, String.class);
        return response.getBody();
    }

    public static String postRequest(String token,String url, JSONObject params) throws Exception {
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add("Accept", "application/json");
        httpHeaders.add("Authorization", "Bearer " + token);
        SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
        requestFactory.setConnectTimeout(10000);// 设置建立连接超时时间
        requestFactory.setReadTimeout(10000);// 设置等待返回超时时间

        RestTemplate client = new RestTemplate(requestFactory);
        HttpMethod method = HttpMethod.POST;
        //httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        HttpEntity<JSONObject> requestEntity = new HttpEntity<>(params, httpHeaders);
        ResponseEntity<String> response = client.exchange(url, method, requestEntity, String.class);
        return response.getBody();
    }

    public static String getRequest(String token,String url, JSONObject params) throws Exception {
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add("Accept", "application/json");
        httpHeaders.add("Authorization", "Bearer " + token);
        SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
        requestFactory.setConnectTimeout(10000);// 设置建立连接超时时间
        requestFactory.setReadTimeout(10000);// 设置等待返回超时时间

        RestTemplate client = new RestTemplate(requestFactory);
        HttpMethod method = HttpMethod.GET;
        //httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        HttpEntity<JSONObject> requestEntity = new HttpEntity<>(params, httpHeaders);
        ResponseEntity<String> response = client.exchange(url, method, requestEntity, String.class);
        return response.getBody();
    }

    public static String postRequestFormUNLENCODED(String url, MultiValueMap<String, String> paramMap) throws Exception {
        HttpHeaders httpHeaders = new HttpHeaders();
        SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
        requestFactory.setConnectTimeout(10000);// 设置建立连接超时时间
        requestFactory.setReadTimeout(10000);// 设置等待返回超时时间

        RestTemplate client = new RestTemplate(requestFactory);
        HttpMethod method = HttpMethod.POST;
        httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(paramMap, httpHeaders);
        //HttpEntity<String> requestEntity = new HttpEntity<>(params.toString(), httpHeaders);
        ResponseEntity<String> response = client.exchange(url, method, requestEntity, String.class);
        return response.getBody();
    }

    public static String postRequestFormUNLENCODED(String url, JSONObject param) throws Exception {
        HttpHeaders httpHeaders = new HttpHeaders();
        SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
        requestFactory.setConnectTimeout(10000); // 设置建立连接超时时间
        requestFactory.setReadTimeout(10000); // 设置等待返回超时时间

        RestTemplate client = new RestTemplate(requestFactory);
        HttpMethod method = HttpMethod.POST;
        httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

        // 将 JSONObject 转换为表单编码的字符串
        StringBuilder formEncodedParams = new StringBuilder();
        for (Map.Entry<String, Object> entry : param.entrySet()) {
            if (formEncodedParams.length() > 0) {
                formEncodedParams.append("&");
            }
            formEncodedParams.append(URLEncoder.encode(entry.getKey(), StandardCharsets.UTF_8.toString()))
                    .append("=")
                    .append(URLEncoder.encode(entry.getValue().toString(), StandardCharsets.UTF_8.toString()));
        }

        HttpEntity<String> requestEntity = new HttpEntity<>(formEncodedParams.toString(), httpHeaders);
        ResponseEntity<String> response = client.exchange(url, method, requestEntity, String.class);
        return response.getBody();
    }

    public static String postRequestFormUNLENCODEDOjbect(String url, MultiValueMap<String, Object> paramMap) throws Exception {
        HttpHeaders httpHeaders = new HttpHeaders();
        SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
        requestFactory.setConnectTimeout(10000);// 设置建立连接超时时间
        requestFactory.setReadTimeout(10000);// 设置等待返回超时时间

        RestTemplate client = new RestTemplate(requestFactory);
        HttpMethod method = HttpMethod.POST;
        httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(paramMap, httpHeaders);
        //HttpEntity<String> requestEntity = new HttpEntity<>(params.toString(), httpHeaders);
        ResponseEntity<String> response = client.exchange(url, method, requestEntity, String.class);
        return response.getBody();
    }

}
